package com.rc.saas.mini.service.rc;

import com.alibaba.fastjson.JSON;
import com.rc.saas.mini.mapper.rc.RcChannelApplyMapper;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.member.MemberInfoExample;
import com.rc.saas.mini.model.member.MemberRefundApply;
import com.rc.saas.mini.model.member.MemberRefundApplyExample;
import com.rc.saas.mini.model.rc.*;
import com.rc.saas.mini.service.member.MemberInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RcChannelApplyServiceImpl implements RcChannelApplyService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private RcChannelApplyMapper rcChannelApplyMapper;

    @Autowired
    private RcMemberFlagService rcMemberFlagService;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private RcChannelLevelSettingService rcChannelLevelSettingService;

    @Autowired
    private RcChannelGainSettingService rcChannelGainSettingService;
    @Autowired
    private RcChannelGainRecordService rcChannelGainRecordService;

    @Override
    public RcChannelApplyMapper getMapper() {
        return rcChannelApplyMapper;
    }

    @Override
    public int insertByReturnId(RcChannelApply rcChannelApply) {
        return rcChannelApplyMapper.insertByReturnId(rcChannelApply);
    }

    @Override
    public List<RcChannelApply> listByCondition(RcChannelApply rcChannelApply) {
        return rcChannelApplyMapper.listByCondition(rcChannelApply);
    }

    /**
     * 拉下升级用户渠道级别
     * @param memberId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRcChannelApplyByMember(Long memberId) {


        RcMemberFlag rcMemberFlag = rcMemberFlagService.selectByPrimaryKey(memberId);
        if("0".equals(rcMemberFlag.getIsChannelLevel())){
            logger.info("用户：{}，未开通渠道商权益：{}", memberId, JSON.toJSONString(rcMemberFlag));
            return;
        }
        MemberInfoExample memberInfoExample=new MemberInfoExample();
        memberInfoExample.createCriteria().andRecommenderIdEqualTo(memberId);
        List<MemberInfo> memberInfoList=memberInfoService.selectByExample(memberInfoExample);

        RcChannelApplyExample rcChannelApplyExample=new RcChannelApplyExample();
        rcChannelApplyExample.createCriteria().andMemberIdEqualTo(memberId).andStatusEqualTo("1").andApplyStatusEqualTo("1");
        RcChannelApply rcChannelApply=rcChannelApplyMapper.selectByExample(rcChannelApplyExample).stream().findFirst().orElseGet(()->null);

        if(rcChannelApply==null){
            logger.info("用户：{}，未开通渠道商权益：{}", memberId, JSON.toJSONString(rcMemberFlag));
            return;
        }

        RcChannelLevelSetting rcChannelLevelSetting=rcChannelLevelSettingService.selectByUpdateCount(memberInfoList.size());
        if (rcMemberFlag.getChannelLevel() != null && rcMemberFlag.getChannelLevel().equals(rcChannelLevelSetting.getChannelLevel())) {
            logger.info("用户：{}，渠道商无变更,不需要升级或者变更：{}", memberId, JSON.toJSONString(rcChannelLevelSetting));
            return;
        }

        if (rcChannelLevelSetting == null) {
            logger.info("用户：{}，渠道商无变更增加：{}", memberId, JSON.toJSONString(rcMemberFlag));
            return;
        }

        ////默认规则
        //List<RcChannelGainSetting> rcChannelGainSettingList = rcChannelGainSettingService.listByDefaultWithChannelLevel(rcChannelLevelSetting.getChannelLevel());
        //if (rcChannelGainSettingList == null || rcChannelGainSettingList.size() == 0) {
        //    //无默认取其他规则
        //    logger.info("用户：{}，渠道商无默认规则：{}", memberId, JSON.toJSONString(rcChannelLevelSetting));
        //    //return;
        //}

        rcMemberFlag.setChannelLevel(rcChannelLevelSetting.getChannelLevel());
        rcMemberFlag.setIsChannelLevel("1");
        rcMemberFlag.setLastUpdatedTime(new Date());
        //会员权益变更
        rcMemberFlagService.updateByPrimaryKey(rcMemberFlag);

        //rcMemberGainRecordService.insertByList(rcMemberGainSettingList, memberId);
        //if (rcChannelGainSettingList.size() > 0) {
        //    //有默认规则自动增加
        //    rcChannelGainRecordService.insertByList(rcChannelGainSettingList, rcChannelApply);
        //}
    }

    @Override
    public RcChannelApply selectByUsingMemberId(Long memberId) {
        RcChannelApplyExample rcChannelApplyExample=new RcChannelApplyExample();
        rcChannelApplyExample.createCriteria().andApplyStatusEqualTo("1").andStatusEqualTo("0").andMemberIdEqualTo(memberId);
        return rcChannelApplyMapper.selectByExample(rcChannelApplyExample).stream().findFirst().orElseGet(()->null);
    }


}
